package jfox.platform.logging.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;

import jfox.platform.common.PaginationList;
import jfox.platform.infrastructure.SuperDAO;
import jfox.platform.logging.entity.Log;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
@Stateless
@Local
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = LogDAOBean.INSERT_LOG,
                query = "INSERT INTO T_SYS_LOG " +
                        "(" +
                        "ID, " +
                        "NAME, " +
                        "PRIORITY, " +
                        "VSTATUS, " +
                        "CREATE_TIME, " +
                        "CREATOR, " +
                        "LAST_MODIFIED, " +
                        "LAST_MODIFIER, " +
                        "DESCRIPTION, " +
                        "MODULE_ID, " +
                        "MODULE_NAME, " +
                        "NODE_ID, " +
                        "NODE_NAME, " +
                        "IP_ADDRESS, " +
                        "TYPE, " +
                        "SUCCESS," +
                        "COMSUMED_MILLISECONDS" +
                        ") " +
                        "VALUES " +
                        "(" +
                        "$LOG.getId(), " +
                        "$LOG.getName(), " +
                        "$LOG.getPriority(), " +
                        "$LOG.getVstatus(), " +
                        "$LOG.getCreateTime(), " +
                        "$LOG.getCreator(), " +
                        "$LOG.getLastModified(), " +
                        "$LOG.getLastModifier(), " +
                        "$LOG.getDescription(), " +
                        "$LOG.getModuleId(), " +
                        "$LOG.getModuleName(), " +
                        "$LOG.getNodeId(), " +
                        "$LOG.getNodeName(), " +
                        "$LOG.getIpAddress(), " +
                        "$LOG.getType(), " +
                        "$LOG.getSuccess()," +
                        "$LOG.getConsumedMilliSeconds()" +
                        ")"
        ),
        @NamedNativeQuery(
                name = LogDAOBean.SELECT_LOG_BY_ID,
                query = "SELECT * FROM T_SYS_LOG WHERE ID=$ID",
                resultClass = Log.class
        ),
        @NamedNativeQuery(
                name = LogDAOBean.DELETE_LOG_BY_ID,
                query = "DELETE FROM T_SYS_LOG WHERE ID=$ID"
        ),
        @NamedNativeQuery(
                name = LogDAOBean.SEARCH_LOG,
                query = "SELECT * FROM T_SYS_LOG " +
                        "WHERE " +
                        "#if($MODULE_ID) MODULE_ID=$MODULE_ID #else MODULE_ID != 0 #end " +
                        "#if($USERNAME) AND CREATOR=$USERNAME #end " +
                        "#if($TYPE) AND TYPE=$TYPE #end " +
                        "#if($START_TIME) AND CREATE_TIME > $START_TIME #end " +
                        "#if($END_TIME) AND CREATE_TIME < $END_TIME #end " +
                        "ORDER BY ID DESC",
                resultClass = Log.class
        ),
                // for sql server
/*
        @NamedNativeQuery(
                name = LogDAOBean.SEARCH_LOG,
                query = "SELECT TOP $LIMIT * FROM T_SYS_LOG " +
                        "WHERE ID NOT IN " +
                        "(SELECT TOP $CURRENTROWS ID FROM T_SYS_LOG " +
                        "WHERE " +
                        "#if($MODULE_ID) MODULE_ID=$MODULE_ID #else MODULE_ID != 0 #end " +
                        "#if($USERNAME) AND CREATOR=$USERNAME #end " +
                        "#if($TYPE) AND TYPE=$TYPE #end " +
                        "#if($START_TIME) AND CREATE_TIME > $START_TIME #end " +
                        "#if($END_TIME) AND CREATE_TIME < $END_TIME #end " +
                        "ORDER BY ID DESC" +
                        ")" +
                        "ORDER BY ID DESC",

                resultClass = Log.class
        ),
*/
        @NamedNativeQuery(
                name = LogDAOBean.COUNT_SEARCH_LOG,
                query = "SELECT COUNT(*) FROM T_SYS_LOG " +
                        "WHERE " +
                        "#if($MODULE_ID) MODULE_ID=$MODULE_ID #else MODULE_ID != 0 #end " +
                        "#if($USERNAME) AND CREATOR=$USERNAME #end " +
                        "#if($TYPE) AND TYPE=$TYPE #end " +
                        "#if($START_TIME) AND CREATE_TIME > $START_TIME #end " +
                        "#if($END_TIME) AND CREATE_TIME < $END_TIME #end ",
                resultClass = int.class
        )


                }
)
public class LogDAOBean extends SuperDAO implements LogDAO {

    public static final String INSERT_LOG = "LogDAOBean.insertLog";
    public static final String SELECT_LOG_BY_ID = "LogDAOBean.getLogById";
    public static final String DELETE_LOG_BY_ID = "LogDAOBean.deleteLog";
    public static final String SEARCH_LOG = "LogDAOBean.searchLog";
    public static final String COUNT_SEARCH_LOG = "LogDAOBean.countSearchLog";

    public Log getLogById(long id) {
        return (Log)getEntityObjectByColumn(SELECT_LOG_BY_ID,"ID",id);
    }

    public int insertLog(Log log) {
        Map<String, Object> paramMap = new HashMap<String, Object>(1);
        paramMap.put("LOG", log);
        return executeNamedNativeUpdate(INSERT_LOG,paramMap);
    }

    public int deleteLog(long logId) {
        return deleteEntityObjectByColumn(DELETE_LOG_BY_ID, "ID", logId);        
    }

    public PaginationList<Log> searchLog(String username, long moduleId, String type, long startTime, long endTime, int start, int limit) {
        Map<String,Object> paramMap = new HashMap<String, Object>(4);
        if(username != null && username.trim().length() != 0) {
            paramMap.put("USERNAME", username);
        }
        if(moduleId != 0) {
            paramMap.put("MODULE_ID", moduleId);
        }
        if(type != null && type.trim().length() !=0) {
            paramMap.put("TYPE", type);
        }
        if(startTime != 0 ) {
            paramMap.put("START_TIME", startTime);
        }
        if(endTime != 0) {
            paramMap.put("END_TIME", endTime);
        }
        paramMap.put("LIMIT", limit);
        paramMap.put("CURRENTROWS", start + limit);
        // 拿到该页的数据
        List<Log> logs = (List<Log>)processNamedNativeQuery(SEARCH_LOG, paramMap, start, limit);
        // 计算数据总量
        int totalCount = (Integer)processNamedNativeQuerySingle(COUNT_SEARCH_LOG, paramMap);
        
        return new PaginationList(logs, totalCount);
    }

    
}
